package com.top.mall.jetpack.vm

import android.app.Application
import android.widget.EditText
import androidx.lifecycle.MutableLiveData
import com.jakewharton.rxbinding2.widget.RxTextView
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.SubscribeViewModel
import com.top.mall.app.SPSConstance
import com.top.mall.base.BaseViewModel
import com.top.mall.entity.HomeIndexResponse
import com.top.mall.entity.UserInfoResponse
import com.top.mall.jetpack.ma.AccessManager
import com.top.mall.jetpack.ma.HomeManager
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function4

class AccessViewModel(application: Application) : BaseViewModel(application) {


    private val mAccessManager: AccessManager = AccessManager()


    val accessLoginLiveData = MutableLiveData<HttpResponseBody<UserInfoResponse>>()
    /**
     * 首页数据
     * */
    fun accessLogin(mobile: String, password: String) {
        setRxLifeScope(object : SubscribeViewModel<UserInfoResponse>(accessLoginLiveData) {
            override fun onSuccess(resp: UserInfoResponse) {
                super.onSuccess(resp.apply {
                    SPSConstance.token = data.userinfo.token
                })
                userIndex()
            }
        }) {
            mAccessManager.accessLogin(mobile, password)
        }
    }


    val isEnabledLoginPwdLiveData = MutableLiveData<Boolean?>()

    /**
     * 登录输入框监听
     * */
    fun observableLoginPwd(editPhone: EditText, editPassword: EditText) {
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(editPhone)
        val observablePassword: Observable<CharSequence> =
            RxTextView.textChanges(editPassword)
        val observable: Disposable = Observable.combineLatest(observablePhone, observablePassword,
            BiFunction<CharSequence, CharSequence, Boolean> { t1, t2 ->
                t1.isNotEmpty() && t2.isNotEmpty()
            })
            .subscribe {
                isEnabledLoginPwdLiveData.postValue(it)
            }
    }

    val isEnabledLoginCheckLiveData = MutableLiveData<Boolean>()

    /**
     * 手机登录输入框监听
     * */
    fun observableLoginCheck(view: EditText) {
        val observable: Disposable =
            RxTextView.textChanges(view).flatMap {
                Observable.just(it.isNotEmpty())
            }.subscribe {
                isEnabledLoginCheckLiveData.postValue(it)
            }
    }

    fun clearLiveDataLoginCheck() {
        this.isEnabledLoginCheckLiveData.postValue(null)
    }


    val contentCheckCodeLiveData = MutableLiveData<CharSequence>()
    val isEnabledCheckCodeLiveData = MutableLiveData<Boolean>()

    /**
     * 验证码输入框监听
     * */
    fun observableCheckCode(view: EditText) {
        val observable: Disposable =
            RxTextView.textChanges(view).doAfterNext {
                contentCheckCodeLiveData.postValue(it)
            }.flatMap {
                Observable.just(it.length == 6)
            }.subscribe {
                isEnabledCheckCodeLiveData.postValue(it)
            }
    }

    fun clearLiveDataCheckCode() {
        this.contentCheckCodeLiveData.postValue(null)
        this.isEnabledCheckCodeLiveData.postValue(null)
    }


    val isEnabledRegisterLiveData = MutableLiveData<Boolean>()

    /**
     * 注册输入框监听
     * */
    fun observableRegister(
        editPhone: EditText,
        editPassword: EditText,
        editPasswordSure: EditText,
        editCaptcha: EditText
    ) {
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(editPhone)
        val observablePassword: Observable<CharSequence> =
            RxTextView.textChanges(editPassword)
        val observablePasswordSure: Observable<CharSequence> =
            RxTextView.textChanges(editPasswordSure)
        val observableCaptcha: Observable<CharSequence> =
            RxTextView.textChanges(editCaptcha)
        val observable: Disposable = Observable.combineLatest(observablePhone,
            observablePassword,
            observablePasswordSure,
            observableCaptcha,
            Function4<CharSequence, CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3, t4 ->
                t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty() && t4.isNotEmpty()
            })
            .subscribe {
                isEnabledRegisterLiveData.postValue(it)
            }
    }
}